<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/4/25
 * Time: 10:00
 */
namespace app\admin\controller;
use think\Db;
class Banner extends Base {
    public function index(){
        if(request()->isAjax()){
            $where = array();
            $params = input('param.');
            if(isset($params['lunbo_status']) && $params['lunbo_status'] != '')
            {
                $where['a.lunbo_status'] = $params['lunbo_status'];
            }
            if(isset($params['lunbo_type']) && $params['lunbo_type'] != '')
            {
                $where['a.lunbo_type'] = $params['lunbo_type'];
            }
//            if(isset($params['key']) && $params['key'] != '')
//            {
//                $where['coupon_name'] = ['like', "%" . $params['key'] . "%"];
//            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('lunbo')->alias('a')
                ->where($where)->count();// 获取总条数
            $lists = Db::name('lunbo')->alias('a')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('a.lunbo_px asc,a.lunbo_status asc')
                ->select();
            foreach ($lists as $key=>&$vo){
                if($vo['lunbo_task_id']!=0||$vo['lunbo_task_id']!=null){
                    $task_name = Db::table('up_task')->where('task_id',$vo['lunbo_task_id'])->value('task_name');
                    $vo['task_name']= $task_name;
                }else{
                    $vo['task_name']= '';
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    //更新排序
    public function edit(){
        extract(input());
        Db::startTrans();// 启动事务
        try{
           Db::table('up_lunbo')->where(['lunbo_id' => $id ])->setField($field , $value);
            Db::commit();// 提交事务
            return ['code' => 200,'data' => '', 'msg' => '排序更新成功'];
        }catch( \Exception $e){
            Db::rollback ();//回滚事务

            return ['code' => 100, 'data' => '', 'msg' => '排序更新失败'];
        }
    }

    //启用
    public function enable(){
        $id = input('param.id');
        extract(input());
        Db::startTrans();// 启动事务
        try{
            Db::table('up_lunbo')->where(['lunbo_id' => $id ])->setField('lunbo_status' , 1);
            Db::commit();// 提交事务
            return ['code' => 200,'data' => '', 'msg' => '操作成功'];
        }catch( \Exception $e){
            Db::rollback ();//回滚事务

            return ['code' => 100, 'data' => '', 'msg' => '操作失败'];
        }
    }
    //禁用
    public function prohibit(){
        $id = input('param.id');
        extract(input());
        $type = Db::table('up_lunbo')->where(['lunbo_id' => $id ])->value('lunbo_type');
        if($type == 3){
            $num1 = Db::table('up_lunbo')
                ->where('lunbo_type',1)
                ->where('lunbo_status',1)
                ->count();
            $num2 = Db::table('up_lunbo')
                ->where('lunbo_type',2)
                ->where('lunbo_status',1)
                ->count();
            if($num1 <= 0){
                return ['code' => 100, 'data' => '', 'msg' => '没有首页轮播图，不能禁用'];
            }
            if($num2 <= 0){
                return ['code' => 100, 'data' => '', 'msg' => '没有任务页轮播图，不能禁用'];
            }
        }else{
            $num = Db::table('up_lunbo')
                ->where('lunbo_type','in',"$type,3")
                ->where('lunbo_status',1)
                ->count();
            if($num <= 1){
                return ['code' => 100, 'data' => '', 'msg' => '没有同类型轮播图，不能禁用'];
            }
        }
        Db::startTrans();// 启动事务
        try{
            Db::table('up_lunbo')->where(['lunbo_id' => $id ])->setField('lunbo_status' , 2);
            Db::commit();// 提交事务
            return ['code' => 200,'data' => '', 'msg' => '操作成功'];
        }catch( \Exception $e){
            Db::rollback ();//回滚事务

            return ['code' => 100, 'data' => '', 'msg' => '操作失败'];
        }
    }

    //添加
    public function add(){
        if(request()->isPost()){
            $lunbo_name = input('param.lunbo_name');
            $lunbo_type = input('param.lunbo_type');
            $lunbo_image = input('param.lunbo_image');
            $lunbo_status = input('param.lunbo_status');
            $lunbo_task_id = input('param.lunbo_task_id');
            $lunbo_px = input('lunbo_px');
            $data['lunbo_name'] = $lunbo_name;
            $data['lunbo_type'] = $lunbo_type;
            $data['lunbo_image'] = $lunbo_image;
            if($lunbo_task_id != ''){
                $data['lunbo_status'] = $lunbo_status;
            }
            $data['lunbo_task_id'] = $lunbo_task_id;
            $data['lunbo_create_time'] = date('Y-m-d H:i:s');
            $data['lunbo_update_time'] = date('Y-m-d H:i:s');
            $data['lunbo_px'] = $lunbo_px;
            $res = Db::table('up_lunbo')->insertGetId($data);
            if($res){
                return json(['code' => 200, 'data' =>"", 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '添加失败']);
            }
        }
        $where['task_status'] = array('neq',5);
        $info = Db::table('up_task')->alias('a')
            ->join('up_shops us','us.s_id = a.task_s_id')
            ->where($where)
            ->field('a.*,us.s_name')
            ->select();
        $this->assign('info',$info);
        return $this->fetch();
    }
    //编辑
    public function update(){
        if(request()->isPost()){
            $id = input('param.id');
            $lunbo_name = input('param.lunbo_name');
            $lunbo_type = input('param.lunbo_type');
//            $lunbo_image = input('param.lunbo_image');
            $lunbo_status = input('param.lunbo_status');
            $lunbo_task_id = input('param.lunbo_task_id');
            $lunbo_px = input('lunbo_px');
            if(!empty(input('lunbo_image'))){
                $data['lunbo_image'] = input('lunbo_image');
            }
            $data['lunbo_name'] = $lunbo_name;
            $data['lunbo_type'] = $lunbo_type;
            $data['lunbo_status'] = $lunbo_status;
            if($lunbo_task_id != ''){
                $data['lunbo_task_id'] = $lunbo_task_id;
            }else{
                $data['lunbo_task_id'] = 0;
            }
            $data['lunbo_update_time'] = date('Y-m-d H:i:s');
            $data['lunbo_px'] = $lunbo_px;
            $res = Db::table('up_lunbo')->where('lunbo_id',$id)->update($data);
            if($res){
                return json(['code' => 200, 'data' =>"", 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '编辑失败']);
            }
        }
        $id = input('param.id');
        $res = Db::table('up_lunbo')->where('lunbo_id',$id)->find();
        $where['task_status'] = array('neq',5);
        $info = Db::table('up_task')->alias('a')
            ->join('up_shops us','us.s_id = a.task_s_id')
            ->where($where)
            ->field('a.*,us.s_name')
            ->select();
        $this->assign('info',$info);
        $this->assign('res',$res);
        return $this->fetch();
    }
    //查看
    public function detail(){
            $id = input('param.id');
            $res = Db::table('up_lunbo')->where('lunbo_id',$id)->find();
            if($res['lunbo_type']==1){
                $res['lunbo_type_name'] = '首页';
                $res['link'] = '无';
            }elseif ($res['lunbo_type']==2){
                $res['lunbo_type_name'] = '任务';
                $task_name = Db::table('up_task')->where('task_id',$res['lunbo_task_id'])->value('task_name');
                $res['link'] = $task_name;
            }elseif ($res['lunbo_type']==3){
                $res['lunbo_type_name'] = '全部';
                $res['link'] = '无';
            }
        if($res['lunbo_status']==1){
            $res['lunbo_status_name'] = '启用';
            }elseif ($res['lunbo_status']==2){
            $res['lunbo_status_name'] = '禁用';
        }
            $this->assign('res',$res);
            return $this->fetch();
    }
}